using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using SpringWaterDataAccess;
using Utilities;

namespace sow
{
	/// <summary>
	/// Summary description for EditPartner.
	/// </summary>
	public partial class PartnerEdit : System.Web.UI.Page
	{
		protected SQLAccess sql; 
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			
			GetSource();
				
			if (!IsPostBack)
			{
				
				if (sql == null)
					sql = new SQLAccess();
				
				DataSet dsUser = new DataSet();
				
				dsUser = sql.GetUsers();
		
				drpUser.DataSource = dsUser;
				drpUser.DataTextField = "NickName";
				drpUser.DataValueField = "UserID";
				drpUser.DataBind();				
				drpUser.SelectedIndex = 0;

				BindData();
				DataGrid1.EditItemIndex = -1;
			}
			
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
			this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
			this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
			this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);

		}
		#endregion

		protected void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			
			DataGrid1.EditItemIndex = e.Item.ItemIndex;
			BindData();			
			lblMessage.Text = "Editing!";
		}

		protected void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			
			int PartnerID = int.Parse(((TextBox)e.Item.Cells[2].Controls[0]).Text);
					
			string PartnerCode =((TextBox)e.Item.Cells[3].Controls[0]).Text; 
			string Description = ((TextBox)e.Item.Cells[4].Controls[0]).Text; 
			//string URL = ((TextBox)e.Item.Cells[5].Controls[0]).Text; 
			string CreatedBy = ((TextBox)e.Item.Cells[5].Controls[0]).Text; 
			//string CreatedDateTime = ((TextBox)e.Item.Cells[7].Controls[0]).Text; 
			//string LastModifiedBy = ((TextBox)e.Item.Cells[8].Controls[0]).Text; 
			//string LastModifiedDateTime = ((TextBox)e.Item.Cells[9].Controls[0]).Text; 
			
			/*
			lblMessage.Text = "The record: --" + PartnerID.ToString() + PartnerCode + 
				Description + URL + CreatedBy + CreatedDateTime + LastModifiedBy + 
				LastModifiedDateTime + "-- has been updated!!!<br>";
			*/
			
			/*
			string sSQL = "Update Partner Set PartnerCode ='" + PartnerCode + 
								"', Description ='" + Description +
								"', URL ='" + URL +
								"', CreatedBy ='" + CreatedBy +
								"', CreatedDateTime ='" + CreatedDateTime +
								"', LastModifiedBy ='" + LastModifiedBy +
								"', LastModifiedDateTime ='" + LastModifiedDateTime +
								"' WHERE PartnerID = " + PartnerID.ToString();
			*/
			
			if (sql == null)
				sql = new SQLAccess();
			sql.UpdatePartner(PartnerID, PartnerCode, Description, CreatedBy);

			DataGrid1.EditItemIndex = -1;
			GetSource();
			BindData();
			
			
		}

		protected void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			DataGrid1.EditItemIndex = -1;
			BindData();
			//DataGrid1.EditItemIndex = -1;
		}

		
		protected void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			int PartnerID =(int)DataGrid1.DataKeys[(int)e.Item.ItemIndex]; 
			if (sql == null)
				sql = new SQLAccess();
			sql.DeletePartner(PartnerID);
			
			lblMessage.Text = "The record of PartnerID = " + PartnerID.ToString() + " is deleted! <br>";
			
			DataGrid1.EditItemIndex = -1;
			GetSource();
			BindData();
		}

		private void GetSource()
		{
			sql = new SQLAccess();
			DataGrid1.DataSource = sql.getAllPartners();
			DataGrid1.DataKeyField = "PartnerID";
			//sqlConnection1.ConnectionString = sql.GetConnectionString();
			//sqlDataAdapter1.Fill(dsPartner1);
		}
		private void BindData()
		{
			DataGrid1.DataBind();
		}

		protected void btnAdd_Click(object sender, System.EventArgs e)
		{
			string sPartnerCode = txtCode.Text;
			string sDesc = txtDesc.Text;
			int UserID = int.Parse(drpUser.SelectedValue);
			string sNickName = drpUser.SelectedItem.ToString();
			string sURL = txtURL.Text;

						
			//lblMessage.Text = sInsert;
			//DataGrid1.CurrentPageIndex = 0;
			
		
			sql = new SQLAccess();
			//sql.InsertPost(sInsert);
			sql.InsertPartner(sPartnerCode, sDesc, sNickName, sURL);
			
			//lblMessage.Text = "Category : " + CategoryID.ToString() + " UserID: " + UserID.ToString() +" Forum: " + forumID.ToString();

			GetSource();
			BindData();
		}

		
	}
}
